#!/bin/bash
alias echo='echo -e'
srcquota="/${QUOTA_TEST}/srcQuotaDir"
srcdir="${FS_MOUNTPOINT}${srcquota}"
destquota="/${QUOTA_TEST}/destQuotaDir"
destdir="${FS_MOUNTPOINT}${destquota}"
noquotadir="${FS_MOUNTPOINT}/${QUOTA_TEST}/dir_noquota"
LOGFILE="/tmp/quotalog"

msg1="目标文件不存在, 源目录和目标都设置配额 (不超出配额)"
msg2="目标文件不存在，源目录和目标都设置配额 (超出配额)"
msg3="目标文件不存在, 源目录<设置>配额，目标目录<没有>配额 (不超出配额)"
msg4="目标文件不存在, 源目录<没有>配额，目标目录<设置>配额 (不超出配额)"
msg5="目标文件不存在, 源目录<没有>配额，目标目录<设置>配额 (超出配额)"
maxmsg=5

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh test_one.sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function reset_all() {
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    func_rmdir ${srcdir}; orcafs-ctl --createdir --nodes=1001 ${srcdir}
    func_rmdir ${destdir}; orcafs-ctl --createdir --nodes=2001 ${destdir}
    echo "=====删除然后重新创建目录 ${srcquota} 和 ${destquota}====="

    # 测试是否两个目录是否在不同 meta 节点
    srcMeta=`orcafs-ctl --getentryinfo ${srcdir} | grep Metadata | awk '{print $3}'`
    descMeta=`orcafs-ctl --getentryinfo ${destdir} | grep Metadata | awk '{print $3}'`
    echo "${srcdir} : ${srcMeta}"
    echo "${destdir} : ${descMeta}"
    if [ ${srcMeta} == ${descMeta} ]; then echo "${srcquota} 和 ${destquota} 在同一个 ${srcMeta}"; exit 1; fi

    orcafs-ctl --setquota --dir=${srcdir} --hardlimit=$1M &> /dev/null # 10
    orcafs-ctl --setquota --dir=${destdir} --hardlimit=$2M &> /dev/null # 5
    sleep ${WAIT_CLIENT_SYNC};

    echo -n "=====重新创建配额目录 ${srcquota}(硬阈值$1MB) 和 ${destquota}(硬阈值$2MB)====="
    quota_num=`orcafs-ctl --listquota | grep -w DIR | wc -l`
    if ((${quota_num} == 2));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function rename_to_unexist_file() {
    eval echo "==${test_num}==\${msg$test_num}===="
    file_size=$(($1 * 1024 * 1024))
    src_file=$srcdir/src_file
    dest_file=$destdir/dest_file

    rm -f $src_file; rm -f $dest_file;
    cmd="dd if=/dev/zero of=$src_file bs=$file_size count=1"; echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${destquota}" | awk '{print $8}'`

    cmd="mv $src_file $dest_file"; echo "\tExec: ${cmd}"; eval ${cmd} &> ${LOGFILE};
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
    if (( ${exceed} == 1 )); then echo "\tDisk quota exceeded, 测试成功"; return; fi
    
    echo ""; echo "  after mv:"
    cmd="ls -l $destdir | grep -v total"; echo -n "\t${cmd}\t>>"; eval ${cmd}

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`
    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${destquota}" | awk '{print $8}'`
    
    src_want=0
    want=$src_want
    echo -n -e "\t源目录,初始用量为 $src_old_used, 期望用量为 $want, 实际为 $src_new_used, "
    if (($src_new_used != $want)); then echo "测试失败"; exit 1; else echo "测试成功"; fi
    
    dest_want=$src_old_used
    want=$dest_want
    echo -n -e "\t目标目录,初始用量为 $dest_old_used, 期望用量为 $want, 实际为 $dest_new_used, "
    if (($dest_new_used != $want)); then echo "测试失败"; exit 1; else echo "测试成功"; fi
}

function rename_file_to_file_v1() {
    eval echo "==${test_num}==\${msg$test_num}===="
    rm -rf ${srcdir}/*; rm -rf ${noquotadir}/*; mkdir ${noquotadir};
    src_file=$srcdir/src_file
    noquota_file=$noquotadir/dest_file

    file_size=$(($1 * 1024 * 1024))
    cmd="dd if=/dev/zero of=$src_file bs=$file_size count=1"; echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    
    src_old_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`
    cmd="mv ${src_file} ${noquotadir}/"; echo "\tExec: ${cmd}"; eval ${cmd} &> ${LOGFILE}

    echo ""; echo "  after mv:"
    cmd="ls -l $noquotadir | grep -v total"; echo -n "\t${cmd}\t>>"; eval ${cmd}
    src_new_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`

    want=0
    echo -n -e "\t源目录,初始用量为 $src_old_used, 期望用量为 $want, 实际为 $src_new_used, "
    if (($src_new_used != $want)); then echo "测试失败"; exit 1; else echo "测试成功"; fi
}

# 源目录没有配额，目标目录设置配额
function rename_file_to_file_v2() {
    eval echo "==${test_num}==\${msg$test_num}===="
    mkdir -p ${noquotadir};  rm -rf ${noquotadir}/*
    mkdir -p ${destdir}; rm -rf ${destdir}/*
    src_file=$noquotadir/src_file
    dest_file=$destdir/dest_file

    file_size=$(($1 * 1024 * 1024))
    cmd="dd if=/dev/zero of=$src_file bs=$file_size count=1"; echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${destquota}" | awk '{print $8}'`
    cmd="mv $src_file $destdir/"; echo "\tExec: ${cmd}"; eval ${cmd} &> ${LOGFILE}
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`

    echo ""; echo "  after mv:"
    cmd="ls -l $destdir | grep -v total"; echo -n "\t${cmd}  >>  "; eval ${cmd}
    if (( ${exceed} == 1 )); then echo "\tDisk quota exceeded, 测试成功"; return; fi
    
    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${destquota}" | awk '{print $8}'`
    want=${file_size}
    echo -n "\t目标目录,初始用量为 $dest_old_used, 期望用量为 $want, 实际为 $dest_new_used, "
    if (($dest_new_used != $want)); then echo "测试失败"; exit 1; else echo "测试成功"; fi
}


echo "/********************************************************************"
echo "测试 rename/mv 文件====源文件和目标文件在不同配额目录, 且目标文件不存在"
echo "********************************************************************/"

if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
source ${QUOTA_COMMON_FUNC}
reset_all 10 5;

test_num=1; if (($1 == ${test_num})) || (($1 == 0));then rename_to_unexist_file 1; fi #1 MB
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then rename_to_unexist_file 7; fi #7 MB
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then rename_file_to_file_v1 1; fi #1 MB
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then rename_file_to_file_v2 1; fi #1 MB
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then rename_file_to_file_v2 7; fi #1 MB

echo "/******************************************************************************"
echo "测试 rename/mv 文件====源文件和目标文件在不同配额目录, 且目标文件不存在 all success"
echo "******************************************************************************/"
exit;



